﻿using gixoom.Dom.Score;

namespace gixoom.Dom.Block
{
    internal class BlockId: IBlockId
    {
        private BlockType blockType;
        private IScore score;
        private string blockIdString;
        private int? hashCode;

        public BlockId(BlockType blockType, IScore score)
        {
            Ensure.ArgumentNotNull(score, "score");

            this.blockType = blockType;
            this.score = score;
        }

        public BlockType BlockType
        {
            get
            {
                return blockType;
            }
        }

        public IScore Score
        {
            get
            {
                return score;
            }
        }

        public override bool Equals(object obj)
        {
            var blockId = obj as BlockId;
            var result = (blockId != null) && (ToString().Equals(blockId.ToString()));

            return result;
        }

        public override int GetHashCode()
        {
            if (!hashCode.HasValue)
            {
                hashCode = ToString().GetHashCode();
            }
            
            return hashCode.Value;
        }

        public override string ToString()
        {
            if (blockIdString == null)
            {
                blockIdString = blockType + " " + score;
            }

            return blockIdString;
        }
    }
}
